WPP.TrustTable = {}

/*==========================================================================
	Name:		WPP_HasTrust
	Purpose: 	Returns true if this player trusts a player for action
==========================================================================*/
function _R.Player:WPP_HasTrust(Player,Key)
	local STR = self:UniqueID() .. Key .. Player:UniqueID()
	return WPP.TrustTable[ STR ] || false
end

/*==========================================================================
	Name:		WPP_SetTrust
	Purpose: 	Set trusts of this player for a player for action
==========================================================================*/
function _R.Player:WPP_SetTrust(Player,Key,Bool)
	local STR = self:UniqueID() .. Key .. Player:UniqueID()
	WPP.TrustTable[ STR ] = Bool
end

/*==========================================================================
	Name:		WPP_HasGlobal
	Purpose: 	Returns true if this player has global trust for action
==========================================================================*/
function _R.Player:WPP_HasGlobal(Key)
	local STR = self:UniqueID() .. Key .. "GBL"
	return WPP.TrustTable[ STR ] || false
end

/*==========================================================================
	Name:		WPP_SetGlobal
	Purpose: 	Sets this players global trust for action
==========================================================================*/
function _R.Player:WPP_SetGlobal(Key,Bool)
	local STR = self:UniqueID() .. Key .. "GBL"
	WPP.TrustTable[ STR ] = Bool
end

/*==========================================================================
	Name:		WPP_AdminCheck
	Purpose: 	Checks if this player can admin another player
				Overwrite for your own admin mod / gamemode
==========================================================================*/
if ( !_R.Player.WPP_AdminCheck ) then
	function _R.Player:WPP_AdminCheck(Player)
		local N = GetConVarNumber("wpp_admin_power")
		local Call = hook.Call("WPP_AdminCheck",GAMEMODE,self,Player,N)
		if ( Call ~= nil ) then
			return Call
		elseif ( N > 0 ) then
			if ( N == 1 ) and ( self:IsAdmin() ) and ( !Player:IsSuperAdmin() ) then
				return true
			elseif ( N == 2 ) and ( self:IsSuperAdmin() ) and ( !Player:IsSuperAdmin() ) then
				return true
			end
		else
			return false
		end
		return false
	end
end

/*==========================================================================
	Name:		WPP_PlayerCheck
	Purpose: 	Checks if this player is trusted by a player
==========================================================================*/
function _R.Player:WPP_PlayerCheck(Player,Key)
	local Call = hook.Call("WPP_PlayerCheck",GAMEMODE,self,Player,Key)
	if ( Call ~= nil ) then
		return Call
	elseif ( self:WPP_AdminCheck(Player) ) or ( Player:WPP_HasGlobal(Key) ) or ( Player:WPP_HasTrust(self,Key) ) then
		////print("Player check Trust")
		return true
	else
		return false
	end
end

/*==========================================================================
	Name:		WPP_PlayerCheck
	Purpose: 	Checks if this player has acces to this entity
==========================================================================*/
function _R.Player:WPP_EntityCheck(Entity,Key)
	if ( !Entity ) then return end
	local Call = hook.Call("WPP_EntityCheck",GAMEMODE,self,Entity,Key)
	if ( Call ~= nil ) then return Call
	elseif ( !Entity:WPP_IsOwned() ) then
		return false //$ToDO: Entiy class and global checks!!
	elseif ( !Entity:WPP_Owner() ) then
		return false
	elseif ( Entity:WPP_IsOwner(self) ) then
		return true
	elseif ( self:WPP_PlayerCheck(Entity:WPP_Owner(),Key) ) or ( Entity.WPP.Share[Key] ) then
		return true
	else
		return false
	end
end

/*==========================================================================
	Name:		WPP_WeaponAsKey
	Purpose: 	Gets the weapon this player is using as an action
==========================================================================*/
function _R.Player:WPP_WeaponAsKey()
	local Weapon = self:GetActiveWeapon()
	if ( !Weapon ) or ( !Weapon:IsValid() ) then return end
	Weapon = Weapon:GetClass()
	
	if ( Weapon == "gmod_tool" ) then
		local Tool = self:GetInfo("gmod_toolmode")
		if ( Tool == "duplicator" ) or ( Tool == "adv_duplicator" ) then
			return "Duplicate"
		elseif ( Tool == "remover" ) then
			return "Remove"
		else
			return "Tool"
		end	
		
	elseif ( Weapon == "weapon_physgun" ) then
		return "Touch"
	elseif ( Weapon == "weapon_physcannon" ) then
		return "Grab"
	end
	
end


/*==========================================================================
	Hook:		WPP_SetTrust -> WPP
	Purpose: 	Syncs up a client with trust
==========================================================================*/
hook.Add("WPP_SetTrust","WPP",function(self,Player,Key,Bool)
	self:WPP_SetTrust(Player,Key,Bool)
	if ( SERVER ) then
		local RF = RecipientFilter( )
		RF:AddPlayer(self)
		RF:AddPlayer(Player)
		
		umsg.Start("WPP_ST")
			umsg.Entity(self)
			umsg.Entity(Player)
			umsg.String(Key)
			umsg.Bool(Bool)
		umsg.End()
	end
end)

/*==========================================================================
	Hook:		WPP_SetGlobal -> WPP
	Purpose: 	Syncs up a client with trust
==========================================================================*/
hook.Add("WPP_SetGlobal","WPP",function(self,Key,Bool)
	self:WPP_SetGlobal(Key,Bool)
	
	if ( SERVER ) then
		umsg.Start("WPP_SG")
			umsg.Entity(self)
			umsg.String(Key)
			umsg.Bool(Bool)
		umsg.End()
	end
end)

	